package com.piz.dao.product;

import java.util.List;

import com.piz.dao.common.PizHiberImpl;
import com.piz.dao.common.UnexpectedDBException;
import com.piz.model.ItemCategory;
import com.piz.model.ItemDetails;
import com.piz.model.common.ItemCategoryEnum;


/**
 * @author prameetk.sharma
 *
 */
@SuppressWarnings("unchecked")
public class ProductDao extends PizHiberImpl{
	
	/**
	 * @return
	 */
	public List<ItemDetails> findAllPizzas()
	{
		try {
			String hqlQueryy = "from ItemCategory c where c.name = 'pizza'";
			ItemCategory pizza = (ItemCategory) executeHQL(hqlQueryy).get(0);
			String hqlQuery = "from ItemDetails pizza where pizza.categoryId = ";
			hqlQuery += pizza.getId();
			return (List<ItemDetails>) executeHQL(hqlQuery);
		} catch (Exception e) {
			throw new UnexpectedDBException("No pizza available.", e);
		}
	}
	
	/**
	 * @param pizzaId
	 * @return
	 */
	public ItemDetails findPizzaById(Long pizzaId)
	{ 
		ItemDetails item;
		try {
			 item = (ItemDetails) search(ItemDetails.class, pizzaId);
		} 
		catch (Exception e) {
			throw new UnexpectedDBException("Item not found.", e);
		}
		
		return item;
	}
	
	/**
	 * @return
	 */
	/**
	 * @return
	 */
	public List<ItemDetails> findAllToppings()
	{
		try {
			String hqlQueryy = "from ItemCategory c where c.name = 'toppings'";
			ItemCategory topp = (ItemCategory) executeHQL(hqlQueryy).get(0);
			String hqlQuery = "from ItemDetails pizza where pizza.categoryId = ";
			hqlQuery += topp.getId();
			List<ItemDetails> tt = (List<ItemDetails>) executeHQL(hqlQuery);
			return tt;
		} catch (Exception e) {
			throw new UnexpectedDBException("No topping available.", e);
		}
		
	}
	
	
	/**
	 * @return
	 */
	public List<ItemDetails> findAllSides()
	{
		try {
			String hqlQueryy = "from ItemCategory c where c.name = '"+"toppings"+"'";
			ItemCategory topp = (ItemCategory) executeHQL(hqlQueryy).get(0);
			
			String hqlQueryyy = "from ItemCategory c where c.name = '"+"Pizza"+"'";
			ItemCategory pizza = (ItemCategory) executeHQL(hqlQueryyy).get(0);
			
			String hqlQuery = "from ItemDetails pizza";
			hqlQuery += " where";
			hqlQuery += " pizza.categoryId !="+ topp.getId();
			hqlQuery += " and";
			hqlQuery += " pizza.categoryId !="+ pizza.getId();
			return (List<ItemDetails>) executeHQL(hqlQuery);
		} catch (Exception e) {
			throw new UnexpectedDBException("No Side Item available.", e);
		}
		
	}
	
	/**
	 * @param type
	 * @param cat
	 * @param taste
	 * @return
	 */
	public List<ItemDetails> fetchProductsByCriteria(Long type, Long cat, Long taste) {
		
		try {
			String hqlQuery = "from ItemDetails item";
			
			if(type != null && type>0.0)
			{
				hqlQuery += " where item.categoryId ="+type;
			}
			if(cat != null && cat>0.0)
			{
				hqlQuery += " and item.classificationId="+cat;
			}
			if(taste != null && taste>0.0)
			{
				hqlQuery += " and item.tasteId="+taste;
			}
			
			return (List<ItemDetails>) executeHQL(hqlQuery);
		} catch (Exception e) {
			throw new UnexpectedDBException("Product with specified criteria not found.", e);
		}
	}
	
	/**
	 * @param id
	 * @return
	 */
	public boolean deleteProduct(Long id) {
		
		ItemDetails item = findPizzaById(id);
		try{
			delete(item);
			return true;
		}
		catch (Exception e) {
			throw new UnexpectedDBException("Error in deleting item", e.getCause());
		}
	}
}
